package net.jxvtc.controller;

import org.apache.log4j.Logger;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import net.jxvtc.pojo.BackendUser;
import net.jxvtc.pojo.DevUser;
import net.jxvtc.service.backenduser.BackendUserService;
import net.jxvtc.tools.Constants;

@Controller
@RequestMapping(value="manager")
public class BackendLoginController {
	private Logger logger = Logger.getLogger(BackendLoginController.class);
	@Resource
	private BackendUserService backendUserService;
	
	@RequestMapping(value="/login")
	public String login(){
		logger.debug("BackendLoginController welcome AppInfo==================");
		return "backendlogin";
	}
	@RequestMapping(value="/dologin",method=RequestMethod.POST)
	public String doLogin(@RequestParam String userCode,@RequestParam String userPassword,HttpServletRequest request,HttpSession session) throws Exception{
		logger.debug("doLogin====================================");
		BackendUser backendUser=backendUserService.login(userCode, userPassword);
		if(null!=backendUser)
		{
			session.setAttribute(Constants.USER_SESSION, backendUser);
			return "redirect:/manager/manager/main";
		}
		else
		{
			request.setAttribute("error", "用户名或密码不正确");
			return "login";
		}
	}
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		//清除session
		session.removeAttribute(Constants.USER_SESSION);
		return "backendlogin";
	}
	
	@RequestMapping(value="/manager/main")
	public String main(){
		return "backend/main";
	}
}
